#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import time
from threading import Thread, RLock, current_thread

# 申请两把互斥锁
obj= RLock()
mutexA = obj
mutexB = obj


class Mythread(Thread):
    def run(self):
        self.t1()
        self.t2()

    def t1(self):
        mutexA.acquire()
        print("线程: {} 获得锁 A".format(current_thread().name))
        mutexB.acquire()
        print("线程: {} 获得锁 B".format(current_thread().name))
        mutexB.release()
        mutexA.release()

    def t2(self):
        mutexB.acquire()
        print("线程: {} 获得锁 B".format(current_thread().name))
        time.sleep(1)
        mutexA.acquire()
        print("线程: {} 获得锁 A".format(current_thread().name))
        mutexA.release()
        mutexB.release()


if __name__ == '__main__':
    for i in range(3):
        t1 = Mythread()
        t1.start()
